import { Result } from './../../model/Result.d';
import { Component, Injector } from '@angular/core';
import { BasePage } from './../../pages/basePage';
import { StorageService } from './../../providers/StorageService';
import { UserService } from './../../providers/UserService';
import { PIC_URL } from "./../../providers/Config";
import { Events } from 'ionic-angular';
declare var $: any;

/**
 * Generated class for the PingjiaComponent component.
 *
 * See https://angular.io/api/core/Component for more info on Angular
 * Components.
 */
@Component({
  selector: 'pingjia',
  templateUrl: 'pingjia.html'
})
export class PingjiaComponent extends BasePage {

  text: string;
  flag:boolean;
  picrooturl = "";//图片服务地址
  userinfo = {}//获取内存中的数据
  orderlist = [];   //获取的待评价信息
  pop=1;    //分页
  bottom1:boolean=false;    //加载全部底部显示器

  constructor(injector: Injector, public userService: UserService, public stc: StorageService,public events: Events) {
    super(injector);
    this.picrooturl = PIC_URL
    this.userinfo = this.stc.read('userinfo');
    this.getMyOrderListAPI('');
    events.subscribe('user:created',(key)=>{
      if(key == 0){
        console.log("全部有数据更新")
        this.again();
      }
    })
  }


  /**获取商品的信息 */
  async getMyOrderListAPI(infiniteScroll) {
    this.stc.removeItem("back");
    let param = { "user_id": this.userinfo['user_id'], "order_type": 2,pop:this.pop };
    let result = await this.userService.getMyOrderListAPI(param);
    if (result.code == 1) {
      this.pop++;
      this.orderlist = this.orderlist.concat(result.data);
      if(this.orderlist.length == 0){
        this.flag=false;
      }else{
        this.flag=true;
      }
      if(infiniteScroll){
        this.bottom1=false;
        infiniteScroll.complete(); //请求完成数据更新页面
        if(result.data.length<6){
          $(".contain2 ion-infinite-scroll-content").css("display","none");
          this.bottom1 = true;
        }
      }
    }else{
      this.flag =false;
      let toast = this.toastCtrl.create({
        message: result.msg,
        duration: 2000,
        position: 'bottom',
      })
      toast.present();
    }  
  }

  //无限滚动
  doInfinite(infiniteScroll) {
    this.bottom1 = false;
    this.getMyOrderListAPI(infiniteScroll);
  }

  /**评论信息填写 */
  comment(item) {
    this.stc.removeItem("back");
    console.log(item);
    let modal = this.modalCtrl.create("CommentPage", {item:item,par:2});
    modal.onDidDismiss(data => {
      if(data=="ok"){
        let key=0;
        this.events.publish("user:created",key)
      }
    });
    modal.present();
  }

  again(){
    this.pop =1;
    this.bottom1 = false;
    this.orderlist=[];
    $(".contain2 ion-infinite-scroll-content").css("display","block");
    this.getMyOrderListAPI('')
  }
}
